package com.dataseek.iot.core.util;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dataseek.iot.core.request.QueryParam;

import java.util.List;
import java.util.function.Consumer;

/**
 * @author cc
 * @Title: 查询条件工具类
 * @Description: 查询条件工具类
 * @date 2021/9/511:07 下午
 * @since 1.0
 */
public class WrapperUtil {

    /**
     * 根据前端回传的逻辑拼装查询条件
     * @param params
     * @return
     */
    public static Wrapper assembleParams(List<QueryParam> params){
        QueryWrapper wrapper = new QueryWrapper();
        params.forEach(new Consumer<QueryParam>() {
            @Override
            public void accept(QueryParam queryParam) {
                String column = queryParam.getColumn();
                String symbol = queryParam.getSymbol();
                Object value  = queryParam.getValue();
                switch (symbol){
                    case "=":
                        wrapper.eq(column, value);
                        break;
                    case ">":
                        wrapper.gt(column, value);
                        break;
                    case "<":
                        wrapper.lt(column, value);
                        break;
                    case ">=":
                        wrapper.ge(column, value);
                        break;
                    case "<=":
                        wrapper.le(column, value);
                        break;
                    case "between":
                        String[] v = value.toString().split(",");
                        wrapper.between(column, v[0], v[1]);
                        break;
                    //TODO 逻辑还没写完
                    default:
                        break;
                }
            }
        });
        return wrapper;
    }


}
